package com.ticket.client;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

/**
 * @Author: lihua
 * @Date: 2024/1/13 23:56
 * @Version: v1.0.0
 * @Description:
 **/
public class User {

    public static void main(String[] args) {
        Socket socket = null;
        DataInputStream inputStream = null;
        DataOutputStream outputStream = null;
        try {
            socket = new Socket("localhost", 8888);
            inputStream = new DataInputStream(socket.getInputStream());
            outputStream = new DataOutputStream(socket.getOutputStream());
            System.out.println("欢迎进入中老铁路订票系统");
            System.out.println("********************************************");
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入用户名:");
            String username = scanner.nextLine();
            boolean isExit = false;
            while (!isExit){
                System.out.println("______________");
                System.out.println("请选择你的操作：\n1 : 查询余票\n2 : 查询自己已购车票\n3 : 购票\n4 : 退票\n5 : 退出");
                String choice = scanner.nextLine();
                String msg;
                switch (choice){
                    case "1":
                        outputStream.writeUTF("1");
                        outputStream.flush();
                        msg = inputStream.readUTF();
                        System.out.println(msg);
                        break;
                    case "2":
                        outputStream.writeUTF("2&"+username);
                        outputStream.flush();
                        msg = inputStream.readUTF();
                        System.out.println(msg);
                        break;
                    case "3":
                        System.out.println("请输入你需要购票的种类：");
                        String ticketId = scanner.nextLine();
                        outputStream.writeUTF("3&"+ticketId+"&"+username);
                        outputStream.flush();
                        msg = inputStream.readUTF();
                        System.out.println(msg);
                        break;
                    case "4":
                        System.out.println("请输入你要退票的票号：");
                        String orederId = scanner.nextLine();
                        outputStream.writeUTF("4&" + orederId);
                        msg = inputStream.readUTF();
                        System.out.println(msg);
                        break;
                    case "5":
                        isExit = true;
                        outputStream.writeUTF("5&");
                        outputStream.flush();
                        System.out.println("客户已退出！");
                        break;
                    default:
                        System.out.println("选择错误，请重新选择!");
                        break;
                }
            }
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
